package com.softeem.servlet;

import com.google.gson.Gson;
import com.softeem.bean.Book;
import com.softeem.bean.CartItem;
import com.softeem.bean.User;
import com.softeem.service.BookService;
import com.softeem.service.CartItemService;
import com.softeem.service.impl.BookServiceImpl;
import com.softeem.service.impl.CartItemServiceImpl;
import com.softeem.utils.BaseServlet;
import com.softeem.utils.Page;
import com.softeem.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

@WebServlet(value="/CartServlet")
public class CartServlet extends BaseServlet {

    protected void updateCountTotalPrice(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException {
        int count = WebUtils.parseInt(req.getParameter("count"),1);
        double totalPrice = WebUtils.parseDouble(req.getParameter("totalPrice"),1.0);
        int id = WebUtils.parseInt(req.getParameter("id"),1);
        CartItemService cartItemService = new CartItemServiceImpl();
        cartItemService.updateCount(id,count,totalPrice);
        //下面回传数据给前端懒的写了
    }

    protected void clearCart(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException {
        CartItemService cartItemService = new CartItemServiceImpl();
        HttpSession session = req.getSession();
        User user =(User)session.getAttribute("user");
        cartItemService.clear(user.getId());
        //下面回传数据给前端懒的写了
    }

    protected void deleteById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException,SQLException{
        Map map = new HashMap();
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter out = resp.getWriter();
        Gson gson = new Gson();
        try {
            CartItemService cartItemService = new CartItemServiceImpl();
            Integer id = WebUtils.parseInt(req.getParameter("id"), 0);
            cartItemService.deleteItem(id);
        }catch (Exception e){
            map.put("msg" , "删除失败啦");
            map.put("code" , 2);

            String str = gson.toJson(map);
            out.print(str);
            return;
        }

        map.put("msg" , "删除成功啦");
        map.put("code" , 1);

        String str = gson.toJson(map);
        out.print(str);
    }


    protected void findCartByUid(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException ,SQLException{
        //从session会话作用域中获取用户的主键id
        User user = (User)req.getSession().getAttribute("user");
        int pageNo = WebUtils.parseInt(req.getParameter("pageNo"),1);
        int pageSize = WebUtils.parseInt(req.getParameter("pageSize"),10);
        CartItemService cartItemService = new CartItemServiceImpl() ;
        Page<CartItem> page = cartItemService.page(pageNo, pageSize, user.getId());
        Gson gson = new Gson();
        //将map 转换成 json 字符串
        String str = gson.toJson(page);

        resp.setContentType("application/json;charset=utf-8");

        PrintWriter out = resp.getWriter();

        out.print(str);
    }

    protected void addItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException,SQLException {

        CartItemService cartItemService = new CartItemServiceImpl();
        // 获取请求的参数 商品编号bookId
        int bookId = WebUtils.parseInt(req.getParameter("id"), 0);

        //从session会话作用域中获取用户的主键id
        User user = (User)req.getSession().getAttribute("user");

        CartItem cart = new CartItem(null, null,1 ,null, null, user.getId());
        //返回封装了商品件数与图书名称的map
        Map map =cartItemService.addItem(cart,bookId);

        Gson gson = new Gson();
        //将map 转换成 json 字符串
        String str = gson.toJson(map);

        resp.setContentType("application/json;charset=utf-8");

        PrintWriter out = resp.getWriter();

        out.print(str);
    }
}
